package cn.touna.jss.modules.third.paipai;

import cn.touna.jss.common.properties.disconf.PaipaiProperties;
import cn.touna.jss.common.util.HttpClientUtils;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.callback.controller.JxlCallbackController;
import cn.touna.jss.modules.third.paipai.entity.FraudInfoEntity;
import cn.touna.jss.modules.third.paipai.util.DigestUtil;
import cn.touna.jss.modules.third.paipai.util.HttpsRequestUtil;
import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import com.alibaba.fastjson.JSONObject;
import com.touna.loan.log.service.TracerContextUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: xiaoli
 * @date: 2019/4/2 13:59
 * @Desc:拍拍信
 **/
@Service
public class PaiPaiService {


    private static final Logger logger = LogManager.getLogger(PaiPaiService.class);


    @Autowired
    PaipaiProperties paipaiProperties;


    @ApiMapping(value = "paipai.fraudInfo", desc = "拍拍信.反欺诈信息查询")
    public Response fraudInfo(FraudInfoEntity entity) {
        Response response = ValidUtil.valid(entity, First.class);
        if (response != null) {
            return response;
        }
        response = new Response();
         String result = HttpsRequestUtil.post(getReqMap(entity),paipaiProperties);
        logger.info("拍拍信 返回结果:{}", result);
        JSONObject jsonObject =  JSONObject.parseObject(result);
        JSONObject resp_body = jsonObject.getJSONObject("resp_body");
        String code = resp_body.getString("result") ;
        String msg = jsonObject.getString("resp_msg");
        if(!code.equals("success")){
            return response.fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_FAIL.getErrorCode(), msg);
        }
        return response.success(jsonObject);
    }



    private Map getReqMap(FraudInfoEntity entity){
        Map map = new HashMap<>();
        map.put("name", entity.getName());
        map.put("idNumber", entity.getIdCard());
        map.put("mobile", entity.getMobile());
        map.put("method", paipaiProperties.getMethod());
        return map;
    }


}
